package assignmentstrategy

import (
	"codejy/analysis/codeanalysis"
	"codejy/globalfunc"
	"codejy/store"
	"codejy/structdef"
	"strings"
)

type CrossFileDefineMethodStrategy struct {
}

func init() {
	RegisterStrategy(&CrossFileDefineMethodStrategy{})
}
func (n CrossFileDefineMethodStrategy) getId() int {
	return codeanalysis.CrossFileDefineMethod
}

func (n CrossFileDefineMethodStrategy) Execute(threadStore *structdef.ThreadStore, code string) string {
	//通过 文件名.行为名称() ,获取值
	//获取第一个点位置
	index := strings.Index(code, ".")
	//截取第一个.之前的字符串,即文件名或者别名
	fileName := code[:index]
	//截取第一个.之后的字符串,即函数调用
	funExpression := code[index+1:]
	return store.RealityFilePathSwitch(threadStore, fileName, func() string {
		interfaces := globalfunc.ExecuteFunc("SubFunctionCallNameAndParam", funExpression).([]interface{})
		funName, funParams := interfaces[0].(string), interfaces[1].([]string)
		globalfunc.ExecuteFunc("FunctionCallExecute", threadStore, funName, funParams)
		//获取函数返回值
		variable1, _ := store.GetVariable(threadStore, funName)
		return variable1.Value
	})
}
